functor Parse (S: PARSE_STRUCTS): PARSE =
struct
  open S
  
  structure Ast = Ast
  
  val error = ErrorMsg.error
  
  structure CLrVals = CLrValsFun(structure Token = LrParser.Token
                                 structure Ast = Ast)
                                 
  structure Lex = CLexFun(structure Tokens = CLrVals.Tokens)
  
  structure CP = Join(structure ParserData = CLrVals.ParserData
		             structure Lex=Lex
		             structure LrParser = LrParser)

  fun parse filename =
      let (* no "Io" exception should be raised when enter from the Compile module
           * When enter from the Main module, a Fail exception should be raised
           * on non-existing files...
           *)
          val file = (TextIO.openIn filename) 
                          handle Io => (print "error: file not found\n"; raise Fail "")
	  fun get _ = TextIO.input file
	  fun parseerror (s,p1,p2) = ErrorMsg.error p1 s
	  val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
	  val result = #1(CP.parse(30, lexer, parseerror, ()))
      in  result
      end handle LrParser.ParseError => (print (String.concat ["Parsing Error on ",
                                                               File.nameToString (!ErrorMsg.file),
                                                               "! Compiling terminated!\n"]);
                                         ErrorMsg.anyErrors := true;
                                         Ast.Program.bogus ())
                        
end
